package com.lscl.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试死信队列
 */
@Configuration
public class Config_05_Dlx {

    // 声明正常的队列,设置死信交换机参数
    @Bean("testQueueDlx")
    public Queue testQueueDlx() {

        Map<String, Object> params = new HashMap();
        // 设置死信交换机
        params.put("x-dead-letter-exchange", "exchange_dlx");

        // 转发给死信交换机的routingKey
        params.put("x-dead-letter-routing-key", "dlx");

        // 队列的过期时间
//        params.put("x-message-ttl",5000);

        // 设置队列的最大长度限制
//        params.put("x-max-length",10);

        return QueueBuilder.durable("test_queue_dlx").withArguments(params).build();
    }

    // 声明死信队列
    @Bean("queue_dlx")
    public Queue queueDlx() {
        return QueueBuilder.durable("queue_dlx").build();
    }


    // 声明死信交换机
    @Bean("test_exchange_dlx")
    public Exchange testExchangeDlx() {
        return ExchangeBuilder.directExchange("exchange_dlx").durable(true).build();
    }

    // 死信交换机绑定死信队列
    @Bean
    public Binding binding() {
        Queue queue = queueDlx();
        Exchange exchange = testExchangeDlx();

        return BindingBuilder.bind(queue).to(exchange).with("dlx").noargs();
    }
}
